package collision;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;

import constans.Global;

import entity.Bullet;
import entity.Entity;
import entity.Hero;
import entity.Unit;

public class CollisionSector {

	public static int  size = 100;
	private Vector2f start = new Vector2f();
	private Vector2f end = new Vector2f();
	
	public boolean isEmpty = true;
	public boolean isUnit = false;
	public boolean isBullet = false;
	
	private Hero hero;
	private Hero movedHero;
	public boolean heroMoved = false;
	public boolean isHero = false;
	
	private List<Unit> unit = new ArrayList<Unit>();
	private List<Unit> movedUnit = new ArrayList<Unit>();
	
	private List<Bullet> bullet = new ArrayList<Bullet>();
	private List<Bullet> movedBullet = new ArrayList<Bullet>();
	
	private List<Entity> removed = new ArrayList<Entity>();
	
	public CollisionSector(int i , int j)
	{
		start.x = i * size;
		start.y = j * size;
		end.x = (i+1) * size;
		end.y = (j+1) * size;	
		if(i == 0) start.x -= size;
		if(j == 0) start.y -= size;
		if(i+1 == Global.MAP_SIZE_X/CollisionSector.size) end.x += size;
		if(j+1 == Global.MAP_SIZE_Y/CollisionSector.size) end.y += size;
	}
	public void render(GameContainer container, Graphics g) throws SlickException
	{	
		for(Unit u : unit)
			u.render(container, g);
		for(Bullet b : bullet)
			b.render(container, g);		
		if(isHero)
			hero.render(container, g);		
	}
	public void update(GameContainer container, int delta) throws SlickException
	{				
		//hero
		if(isHero)
		{
			hero.update(container, delta);
			if(hero.isBullet())
				addBullet(hero.shoot());
				
			if(!isOn(hero))
				moveHero(hero);
		}
		//units
		if(isUnit)
			for(Unit u : unit)
			{
				u.update(container, delta);
				if(!isOn(u))
					moveUnit(u);	
				if(u.isDead())
					remove(u);
				
				if(u.isBullet())
					addBullet(u.shoot());
			}
		//bullet
		if(isBullet)
			for(Bullet b : bullet)
			{
				b.update(container, delta);
				if(!isOn(b))
					moveBullet(b);	
				else if(b.isDead())
					remove(b);
			}
		clear();
	}
	public boolean isOn(Entity e)
	{
		if(e.position.x >= start.x && e.position.x < end.x)
			if(e.position.y >= start.y && e.position.y < end.y)
				return true;
		return false;
	}
	public void remove(Entity e)
	{
		removed.add(e);
	}
	private void clear()
	{
		for(Entity e : removed)
		{
			unit.remove(e);
			bullet.remove(e);
		}
		removed.clear();
	
		for(Unit e : movedUnit)
			unit.remove(e);	
		for(Bullet b : movedBullet)
			bullet.remove(b);
				
		isEmpty();		
	}
	private void isEmpty()
	{
		if(bullet.isEmpty())
			isBullet = false;
		else isBullet = true;
		if(unit.isEmpty())
			isUnit = false;	
		else isUnit = true;		
		
		if(!isBullet && !isHero && !isUnit)
			isEmpty = true;
		else isEmpty = false;
	}
	//hero
	public void moveHero(Hero h)
	{
		isHero = false;
		heroMoved = true;
		movedHero = hero;
	}
	public void addHero(Hero hero2)
	{
		isEmpty = false;
		isHero = true;
		hero = hero2;                                                                                                                                                                                                                                                                                                                                                                                                            
	}
	public Hero getHero()
	{
		return hero;
	}
	public Hero getMovedHero()
	{
		heroMoved = false;
		return movedHero;
	}
	//unit
	public void addUnit(Unit u)
	{
		isEmpty = false;
		isUnit = true;
		unit.add(u);
	}
	public void moveUnit(Unit e)
	{
		movedUnit.add(e);
	}
	public List<Unit> getMovedUnit()
	{
		List<Unit> tmp = new ArrayList<Unit>();
		tmp.addAll(movedUnit);
		movedUnit.clear();
		return tmp;
	}
	public void setTarget(Vector2f t)
	{
		for(Unit u : unit)
			u.setTarget(t);
	}
	public List<Unit> getUnits()
	{
		return unit;
	}
	public void takeDamage(Unit u, float d)
	{
		unit.get(unit.indexOf(u)).takeDamage(d);
	}
	//bullet	
	public void addBullet(Bullet b)
	{
		isEmpty = false;
		isBullet = true;
		bullet.add(b);
	}
	public void moveBullet(Bullet e)
	{
		movedBullet.add(e);
	}
	public List<Bullet> getMovedBullet()
	{
		List<Bullet> tmp = new ArrayList<Bullet>();
		tmp.addAll(movedBullet);
		movedBullet.clear();
		return tmp;
	}
	public List<Bullet> getBullets()
	{
		return bullet;
	}
	public void detonate(Bullet b)
	{
		bullet.get(bullet.indexOf(b)).detonate();
	}
}
